package com.zifeng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zifeng.common.R;
import com.zifeng.dao.AdministorDao;
import com.zifeng.pojo.Administor;
import com.zifeng.pojo.AdministorDto;
import com.zifeng.service.IAdministorService;
import com.zifeng.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * 船舶管理员Controller
 *
 * @author zifeng
 * @date 2025-03-27
 */
@RestController
@RequestMapping("/administrator")
public class AdministratorController {
    @Autowired
    private IAdministorService administorService;

    @Autowired
    private AdministorDao administorDao;


    @RequestMapping("/listByFuzzyQuery")
    public R listByFuzzyQuery(@RequestParam Map<String, String> params) throws IOException {

        String data = params.get("data");
        if (StringUtils.isEmpty(data)) {
            return R.error(401, "查询参数不能为空");
        }
        ObjectMapper om = new ObjectMapper();
        Administor administor = om.readValue(data, Administor.class);
        List<Administor> list = administorDao.fuzzyQuery(administor);
        int currPage = Integer.parseInt(params.get("page"));
        int pageSize = Integer.parseInt(params.get("limit"));
        PageUtils page = new PageUtils(list, list.size(), pageSize, currPage);
        return R.ok().put("page", page);
    }


    /**
     * 网站管理：列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = administorService.queryPage(params);
        page.setTotalCount(page.getList().size());
        page.setTotalPage((int) Math.ceil(1.0 * page.getList().size() / page.getPageSize()));
        int currPage = page.getCurrPage();
        int pageSize = page.getPageSize();
        //从第几条数据开始
        int firstIndex = (currPage - 1) * pageSize;
        //到第几条数据结束
        int lastIndex = currPage * pageSize;
        //当最后索引大于总数时，要进行设置最后索引
        if (lastIndex > page.getTotalCount()) {
            lastIndex = page.getTotalCount();
        }
        //当只有一条数据时
        if (lastIndex == firstIndex) {
            page.setList(page.getList());
        }else {
            page.setList(page.getList().subList(firstIndex, lastIndex));
        }

        return R.ok().put("page", page);
    }

    /**
     * 登录
     */
    @RequestMapping("/login")
    public R login(@RequestParam String username, @RequestParam String password){
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return R.error().put("fail", "账号或密码为空");
        }
        LambdaQueryWrapper<Administor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Administor::getUsername, username)
                .eq(Administor::getPassword, password);
        Administor administorEntity = administorService.getOne(queryWrapper);
        if (administorEntity == null || administorEntity.getId() == 0) {
            return R.error().put("fail", "账号或密码错误");
        }
        String token = UUID.randomUUID().toString();
        AdministorDto administorDto = new AdministorDto();
        administorDto.setAdministor(administorEntity);
        administorDto.setToken(token);
        return R.ok().put("administorDto", administorDto);
    }

    @GetMapping(value = "/{id}")
    public R getAdministor(@PathVariable("id") Long id)
    {
        Administor administor = administorDao.selectById(id);
        return R.ok().put("administor", administor);
    }

    /**
     * 保存
     */
    @PostMapping
    public R add(@RequestBody Administor administor){
		administorService.save(administor);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    public R edit(@RequestBody Administor administor){
		administorService.updateById(administor);

        return R.ok();
    }

    /**
     * 网站管理：删除
     */
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids)
    {
        int i = administorDao.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

}
